package com.powergame.controls;

import com.jme3.collision.CollisionResults;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import java.io.IOException;

/**
 * CameraControl.java
 *
 * Controle le déplacement de la caméra en 2d
 *
 * @author ToF
 */
public class CameraControl implements Control, AnalogListener, ActionListener {
    
    public static float MOVE_SPEED = 30f;
    
    private InputManager inputManager = null;
    private Camera cam = null;
    
    private float xMin = 0;
    private float xMax = 0;
    private float yMin = 0;
    private float yMax = 0;
    private float zMin = 0;
    private float zMax = 0;
    
    public CameraControl(Camera camera){
        this.cam = camera;
    }
    
    public Control cloneForSpatial(Spatial spatial) {
        return null;
    }

    public void setSpatial(Spatial spatial) {
    }

    public void update(float tpf) {
    }

    public void render(RenderManager rm, ViewPort vp) {
    }

    public void write(JmeExporter ex) throws IOException {
    }

    public void read(JmeImporter im) throws IOException {
    }

    public float getxMax() {
        return xMax;
    }

    public void setxMax(float xMax) {
        this.xMax = xMax;
    }

    public float getxMin() {
        return xMin;
    }

    public void setxMin(float xMin) {
        this.xMin = xMin;
    }

    public float getyMax() {
        return yMax;
    }

    public void setyMax(float yMax) {
        this.yMax = yMax;
    }

    public float getyMin() {
        return yMin;
    }

    public void setyMin(float yMin) {
        this.yMin = yMin;
    }

    public float getzMax() {
        return zMax;
    }

    public void setzMax(float zMax) {
        this.zMax = zMax;
    }

    public float getzMin() {
        return zMin;
    }

    public void setzMin(float zMin) {
        this.zMin = zMin;
    }

    /**
     * Configuration des touches et boutons pour le control de la caméra
     * 
     * @param inputManager Gestionnaire JME
     */
    public void registerInput(InputManager inputManager){
        
        String[] mappings = {"UP","DOWN","LEFT","RIGHT","ZOOM_IN","ZOOM_OUT"};
        
        inputManager.addMapping("UP", new KeyTrigger(KeyInput.KEY_Z), new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping("DOWN", new KeyTrigger(KeyInput.KEY_S), new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addMapping("LEFT", new KeyTrigger(KeyInput.KEY_Q), new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping("RIGHT", new KeyTrigger(KeyInput.KEY_D), new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping("ZOOM_IN", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("ZOOM_OUT", new KeyTrigger(KeyInput.KEY_X));
        
        inputManager.addListener(this, mappings);
    }

    public void onAction(String name, boolean isPressed, float tpf) {
    }

    public void onAnalog(String name, float value, float tpf) {
        if(name.equals("UP")){
            if(cam.getLocation().z >= zMin){
                cam.setLocation(cam.getLocation().add(new Vector3f(0,0,-1 * MOVE_SPEED * tpf)));
            }
        }
        if(name.equals("DOWN")){
            if(cam.getLocation().z <= zMax){
                cam.setLocation(cam.getLocation().add(new Vector3f(0,0, MOVE_SPEED * tpf)));
            }
        }
        if(name.equals("LEFT")){
            if(cam.getLocation().x >= xMin){
                cam.setLocation(cam.getLocation().add(new Vector3f(-1 * MOVE_SPEED * tpf,0,0)));
            }
        }
        if(name.equals("RIGHT")){
            if(cam.getLocation().x <= xMax){
                cam.setLocation(cam.getLocation().add(new Vector3f(MOVE_SPEED * tpf,0,0)));
            }
        }
        if(name.equals("ZOOM_IN")){
            if(cam.getLocation().y >= yMin){
                cam.setLocation(cam.getLocation().add(new Vector3f(0, -1 * MOVE_SPEED * tpf,0)));
            }
        }
        if(name.equals("ZOOM_OUT")){
            if(cam.getLocation().y <= yMax){
                cam.setLocation(cam.getLocation().add(new Vector3f(0, MOVE_SPEED * tpf,0)));
            }
        }
    }

    public void setEnabled(boolean enabled) {
        
    }

    public boolean isEnabled() {
        return true;
    }
}
